package com.yz.rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.channels.Channels;
import java.util.concurrent.TimeoutException;

/**
 * 描述：
 **/
public class ChannelsUtil {

   public static Channel getChannel() {
       ConnectionFactory connectionFactory = new ConnectionFactory();
       //connectionFactory.setHost("192.168.220.130");
       //connectionFactory.setPort(5672);
       connectionFactory.setHost("111.229.85.126");
       connectionFactory.setPort(5671);
       connectionFactory.setVirtualHost("test");
       connectionFactory.setPassword("123123");
       connectionFactory.setUsername("zhang");
       try {
           Connection connection = connectionFactory.newConnection();
           Channel channel = connection.createChannel();
           return channel;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return null;
   }

   public static void closeChannel(Channel channel){
       try {
           channel.close();
           channel.getConnection().close();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (TimeoutException e) {
           e.printStackTrace();
       }

   }

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.220.128");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("zhang");
        connectionFactory.setPassword("123123");
        connectionFactory.setVirtualHost("/");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
    }
}
